package com.demo.autotest.controller;

import com.demo.autotest.common.constant.MenuEnum;
import com.demo.autotest.database.mapper.PermissionMapper;
import com.demo.autotest.dto.MenuDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController // 定义一个controller类
@RequestMapping("/autotest")
public class PermissionController {

    @Resource
    private PermissionMapper permissionMapper;

    @GetMapping("/menu/list")
    public List<MenuDTO> getMenu(@RequestParam String userId, @RequestParam String projectId){
        List<MenuDTO> result = new ArrayList<>();
        List<String> permissionList = permissionMapper.getPermissionByProjectAndUser(projectId, userId);
        for (MenuEnum father: MenuEnum.values()){
            if (father.parentId == null){
                List<MenuDTO> menus = new ArrayList<>();
                for(MenuEnum son: MenuEnum.values()){
                    if (Objects.equals(son.parentId, father.id) && permissionList.contains(son.permissionId)){
                        MenuDTO menuDTO = new MenuDTO();
                        menuDTO.setId(son.id);
                        menuDTO.setName(son.name);
                        menuDTO.setIcon(son.icon);
                        menuDTO.setPath(son.path);
                        menus.add(menuDTO);
                    }
                }
                if (menus.size()>0){
                    MenuDTO menuDTO = new MenuDTO();
                    menuDTO.setId(father.id);
                    menuDTO.setName(father.name);
                    menuDTO.setIcon(father.icon);
                    menuDTO.setMenus(menus);
                    result.add(menuDTO);
                }
            }
        }
        return result;
    }

}
